<?php
/***
 * Candy框架 获取客户端信息类	
 * 
 * $Author: 刘森 (fingerboy@qq.com) $
 * $Date: 2019-08-05 01:28:06 $   
 */

declare(strict_types=1);
namespace Candy\Extend\Network;

defined('CANDY') OR die('You Are A Bad Guy. o_O???');

final Class Client {
	/**
	 * 获取客户端系统语言
	 * 
	 * @access public
	 * @return string
	 */
	public static function getUserLang(): string
	{
		if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
			return htmlspecialchars($_SERVER['HTTP_ACCEPT_LANGUAGE']);
		} 
		return '';
	}
	
	/**
	 * 获取客户端IP地址
	 *
	 * @param integer $data['type'] 返回类型 0 返回IP地址 1 返回IPV4地址数字
	 * @param boolean $adv 是否进行高级模式获取（有可能被伪装） 
	 * @return mixed
	 */
	public static function getClientIp(int $type = 0,bool $adv = false): string
	{
		static $ip = [];
		if(is_null(\session('clientip'))){
			if(empty($ip)){
				$type = $type ? 1 : 0;
				if($adv){
					if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
						$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
						$pos = array_search('unknown',$arr);
						if(false !== $pos) unset($arr[$pos]);
						$ipmark = trim($arr[0]);
					}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
						$ipmark = $_SERVER['HTTP_CLIENT_IP'];
					}elseif (isset($_SERVER['REMOTE_ADDR'])) {
						$ipmark = $_SERVER['REMOTE_ADDR'];
					}
				}elseif (isset($_SERVER['REMOTE_ADDR'])) {
					$ipmark = $_SERVER['REMOTE_ADDR'];
				}
				// IP地址合法验证
				$long = sprintf('%u',ip2long($ipmark));
				$ip   = $long ? [$ipmark, $long] : ['127.0.0.1', 0];
			}
			\session('clientip', $ip[$type]);
		}
		return \session('clientip');
	}
	
	/**
	 * 获取客户端地址
	 *
	 * @param string $ip IPV4地址数字
	 * @return srting
	 */
	public static function getLocation(string $ip = ''): string
	{
		static $__location = [];
		empty($ip) && $ip = self::getClientIp();
		if($ip == '127.0.0.1') return '本机地址';
		if(isset($__location[$ip])) return $__location[$ip];
		
		$api = 'http://whois.pconline.com.cn/ip.jsp?ip='.$ip; //太平洋接口
		$_location = @file_get_contents($api); 
		
		//自由接口
		if(empty($_location)){
			$api = API_URL . '/Api/getLocation.shtml?ip='.$ip;
			$_location = @file_get_contents($api);
		}else{
			$_location = str_replace(' ', '', trim(mConvtEncod($_location)));
		}
		$__location[$ip] = $_location;
		return $_location;
	}
	
	/**
	 * 获取当前页面的url来源
	 * 
	 * @access public
	 * @return string
	 */
	public static function getUrlSource(): string
	{
		if (isset($_SERVER['HTTP_REFERER'])) {
			return htmlspecialchars($_SERVER['HTTP_REFERER']);
		} 
		return '';
	}

	/**
	 * 获取客户端浏览器信息.
	 * 
	 * @access public
	 * @return string
	 */
	public static function getUserAgent(): string
	{
		if (isset($_SERVER['HTTP_USER_AGENT'])) {
			return htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
		} 
		return '';
	}

	/**
	 * 获取客户端浏览器信息
	 * 
	 * @access public
	 * @return string
	 */
	public static function getUserBrowser(): string
	{
		$userAgentInfo = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
		if(strpos($userAgentInfo,'MSIE 9.0')) {
			return 'IE9';
		}else if(strpos($userAgentInfo,'MSIE 8.0')) {
			return 'IE8';
		}else if(strpos($userAgentInfo,'MSIE 7.0')) {
			return 'IE7';
		}else if(strpos($userAgentInfo,'MSIE 6.0')) {
			return 'IE6';
		}else if(strpos($userAgentInfo,'Firefox')) {
			return 'Firfox';
		}else if(strpos($userAgentInfo,'Chrome')) {
			return 'Chrome';
		}else if(strpos($userAgentInfo,'Opera')) {
			return 'Opera';
		}else if(strpos($userAgentInfo,'Safari')) {
			return 'Safari';
		}else if(strpos($userAgentInfo,'Elinks')) {
			return 'Elinks';
		}else if(strpos($userAgentInfo,'OmniWeb')) {
			return 'OmniWeb';
		}else if(strpos($userAgentInfo,'Links')) {
			return 'Links';
		}else if(strpos($userAgentInfo,'Lynx')) {
			return 'Lynx';
		}else if(strpos($userAgentInfo,'Arora')) {
			return 'Arora';
		}else if(strpos($userAgentInfo,'Epiphany')) {
			return 'Epiphany';
		}else if(strpos($userAgentInfo,'Konqueror')) {
			return 'Konqueror';
		}else if(strpos($userAgentInfo,'EudoraWeb')) {
			return 'EudoraWeb';
		}else if(strpos($userAgentInfo,'Minimo')) {
			return 'Minimo';
		}else if(strpos($userAgentInfo,'NetFront')) {
			return 'NetFront';
		}else if(strpos($userAgentInfo,'POLARIS')) {
			return 'Polaris';
		}else if(strpos($userAgentInfo,'BlackBerry')) {
			return 'BlackBerry';
		}else if(strpos($userAgentInfo,'Nokia')) {
			return 'Nokia';
		}else if(strpos($userAgentInfo,'360SE')) {
			return '360安全浏览器';
		}else if(strpos($userAgentInfo,'SE 2.x')) {
			return '搜狗浏览器';
		}else if(strpos($userAgentInfo,'MicroMessenger')) {
			return '微信浏览器';
		}else{
			return 'Others';
		}
	}

	/**
	 * 获取客户端操作系统信息
	 * 
	 * @access public
	 * @return string
	 */
	public static function getUserOS(): string
	{
		$userAgentInfo = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
		if(strpos($userAgentInfo,'Windows NT 6.1')) {
			return 'Windows 7';
		}else if(strpos($userAgentInfo,'Windows NT 6.0')) {
			return 'Windows Vista';
		}else if(strpos($userAgentInfo,'Windows NT 5.2')) {
			return 'Windows 2003';
		}else if(strpos($userAgentInfo,'Windows NT 5.1')) {
			return 'Windows XP';
		}else if(strpos($userAgentInfo,'Windows NT 5.0')) {
			return 'Windows 2000';
		}else if(strpos($userAgentInfo,'Windows ME')) {
			return 'Windows ME';
		}else if(strpos($userAgentInfo,'PPC Mac OS X')) {
			return 'OS X PPC';
		}else if(strpos($userAgentInfo,'Intel Mac OS X')) {
			return 'OS X Intel';
		}else if(strpos($userAgentInfo,'Win98')) {
			return 'Windows 98';
		}else if(strpos($userAgentInfo,'Win95')) {
			return 'Windows 95';
		}else if(strpos($userAgentInfo,'WinNT4.0')) {
			return 'Windows NT4.0';
		}else if(strpos($userAgentInfo,'Mac OS X Mach-O')) {
			return 'OS X Mach';
		}else if(strpos($userAgentInfo,'Ubuntu')) {
			return 'Ubuntu';
		}else if(strpos($userAgentInfo,'Debian')) {
			return 'Debian';
		}else if(strpos($userAgentInfo,'AppleWebKit')) {
			return 'WebKit';
		}else if(strpos($userAgentInfo,'Mint/8')) {
			return 'Mint 8';
		}else if(strpos($userAgentInfo,'Minefield')) {
			return 'Minefield Alpha';
		}else if(strpos($userAgentInfo,'gentoo')) {
			return 'Gentoo';
		}else if(strpos($userAgentInfo,'Kubuntu')) {
			return 'Kubuntu';
		}else if(strpos($userAgentInfo,'Slackware/13.0')) {
			return 'Slackware 13';
		}else if(strpos($userAgentInfo,'Fedora')) {
			return 'Fedora';
		}else if(strpos($userAgentInfo,'FreeBSD')) {
			return 'FreeBSD';
		}else if(strpos($userAgentInfo,'SunOS')) {
			return 'SunOS';
		}else if(strpos($userAgentInfo,'OpenBSD')) {
			return 'OpenBSD';
		}else if(strpos($userAgentInfo,'NetBSD')) {
			return 'NetBSD';
		}else if(strpos($userAgentInfo,'DragonFly')) {
			return 'DragonFly';
		}else if(strpos($userAgentInfo,'IRIX')) {
			return 'IRIX';
		}else if(strpos($userAgentInfo,'Windows CE')) {
			return 'Windows CE';
		}else if(strpos($userAgentInfo,'PalmOS')) {
			return 'PalmOS';
		}else if(strpos($userAgentInfo,'Linux')) {
			return 'Linux';
		}else if(strpos($userAgentInfo,'DragonFly')) {
			return 'DragonFly';
		}else if(strpos($userAgentInfo,'Android')) {
			return 'Android';
		}else if(strpos($userAgentInfo,'Mac OS X')) {
			return 'Mac OS X';
		}else if(strpos($userAgentInfo,'iPhone')) {
			return 'iPhone OS';
		}else if(strpos($userAgentInfo,'Symbian OS')) {
			return 'Symbian';
		}else if(strpos($userAgentInfo,'Symbian OS')) {
			return 'Symbian';
		}else if(strpos($userAgentInfo,'SymbianOS')) {
			return 'SymbianOS';
		}else if(strpos($userAgentInfo,'webOS')) {
			return 'webOS';
		}else if(strpos($userAgentInfo,'PalmSource')) {
			return 'PalmSource';
		}else{
			return 'Others';
		}		
	}
}
